<!doctype html>
<html lang="zh-CN"><head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script>
    <title>薛尧的博客</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="" />

    
    
    
    <link rel="stylesheet" href="../../../css/theme.min.css">

    
    
    
    
    <link rel="stylesheet" href="../../../css/custom.min.css">
    

    
</head>
<body>
        <div id="content" class="mx-auto"><header class="container mt-sm-5 mt-4 mb-4 mt-xs-1">
    <div class="row">
        
        <div class="col-sm-4 col-12 text-sm-right text-center pt-sm-4">
            <a href="../../../" class="text-decoration-none">
                <img id="home-image" class="rounded-circle"
                    
                        src="https://ueyao.github.io/image-hosting/blog/avatar.png"
                    
                />
            </a>
        </div>
        <div class="col-sm-8 col-12 text-sm-left text-center">
        
            <h2 class="m-0 mb-2 mt-4">
                <a href="../../../" class="text-decoration-none">
                    
                        薛尧
                    
                </a>
            </h2>
            <p class="text-muted mb-1">
                
                    Java Developer | Short Video Creator
                
            </p>
            <ul id="nav-links" class="list-inline mb-2">
                
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../about/" title="关于">关于</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../post/" title="文章">文章</a>
                    </li>
                
                    <li class="list-inline-item">
                        <a class="badge badge-white " href="../../../categories/" title="分类">分类</a>
                    </li>
                
            </ul>
            <ul id="nav-social" class="list-inline">
                
                    <li class="list-inline-item mr-3">
                        <a href="http://github.com/flowstone" target="_blank">
                            <i class="fab fa-github fa-1x text-muted"></i>
                        </a>
                    </li>
                
                    <li class="list-inline-item mr-3">
                        <a href="mailto:xueyao.me#gmail.com" target="_blank">
                            <i class="fas fa-at fa-1x text-muted"></i>
                        </a>
                    </li>
                
            </ul>
        </div>
    </div>
    <hr />
</header>
<div class="container">
    <div class="pl-sm-2">
        <div class="mb-3">
            <h3 class="mb-0">Docker进阶-资源管理Swarm&#43;Portainer</h3>
            
            <small class="text-muted">发布于 2018-09-07</small>
        </div>

        <article>
            <h2 id="docker-swarm资源管理">Docker Swarm资源管理</h2>
<p>Docker Swarm是Docker官方三剑客项目之一，提供Docker容器集群服务，是Docker官方对容器云生态进行支持的核心方案。</p>
<p>使用它，用户可以将多个Docker主机封装为单个大型的虚拟Docker主机，快速打造一套容器云平台。</p>
<p>注意：Docker1.12.0之后版本，Swarm模块已经内嵌入Docker引擎，成为Docker子命令docker swarm,绝大多用户已经开始使用Swarm模块，Docker引擎API已经删除Docker Swarm。</p>
<h3 id="基本概念">基本概念</h3>
<p>Swarm是使用SwarmKit构建的Docker引擎内置(原生)的集群管理和编排工具。使用Swarm集群之前需要了解以下几个概念。</p>
<h4 id="节点">节点</h4>
<p>运行Docker的主机可以主动初始化一个Swarm集群或者加入一个已存在的Swarm集群，这样运行Docker的主机就成为一个Swarm集群的节点(node)。</p>
<p>节点分为管理(manager)节点和工作(worker)节点。</p>
<ul>
<li>
<p>管理节点用于Swarm集群的管理，docker swarm集合基本只能在管理节点执行。</p>
</li>
<li>
<p>工作节点是任务执行节点，管理节点将服务(service)下发至工作节点执行。</p>
</li>
</ul>
<p>集群中管理节点与工作节点的关系</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-resource-manage-01.png" alt=""></p>
<h4 id="服务和任务">服务和任务</h4>
<p>任务(Task)是Swarm中的最小的调度单位，目前来说就是一个单一的容器。</p>
<p>服务(Services)是指一组任务的集合，服务定义了任务的属性。</p>
<p>服务有两种模式：</p>
<ul>
<li>
<p>replicated services 按照一定规则在各个工作节点上运行指定个数的任务。</p>
</li>
<li>
<p>global services每个工作节点运行一个任务</p>
</li>
</ul>
<p>两个模式通过docker service create的&ndash;mode参数指定</p>
<p>容器、任务、服务的关系</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-resource-manage-02.png" alt=""></p>
<h3 id="创建swarm集群">创建Swarm集群</h3>
<p>了解Swarm集群由管理节点和工作节点组成后，我们创建一个包含一个管理节点和两个工作节点的最小Swarm集群。</p>
<h4 id="初始化集群">初始化集群</h4>
<p>使用docker swarm init在本地初始化一个Swarm集群。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker swarm init --advertise-addr 192.168.1.1
</span></span></code></pre></div><p>如果你的Docker主机有多个网段，拥有多个IP，必须使用&ndash;advertise-addr指定IP。执行docker swarm init命令的节点自动成为管理节点。</p>
<p>注意：使用docker swarm init</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-resource-manage-03.png" alt=""></p>
<h4 id="增加工作节点">增加工作节点</h4>
<p>在另外两台服务器上执行上一步创建管理节点时候的输出的加入swarm集群的全集</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker swarm join <span style="color:#ae81ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#ae81ff"></span>--token SWMTKN-1-3pu6hszjas19xyp7ghgosyx9k8atbfcr8p2is99znpy26u2lkl-1awxwuwd3z9j1z3puu7rcgdbx <span style="color:#ae81ff">\ </span>
</span></span><span style="display:flex;"><span>192.168.1.1:2377
</span></span></code></pre></div><h4 id="查看集群">查看集群</h4>
<p>在管理节点使用docker node ls查看集群。</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker node ls
</span></span></code></pre></div><p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-resource-manage-04.png" alt=""></p>
<h3 id="部署服务">部署服务</h3>
<p>使用docker service命令来管理Swarm集群中的服务，该命令只能在管理节点运行。</p>
<h4 id="新建服务">新建服务</h4>
<p>在创建好的Swarm集群中运行nginx服务</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker service create --replicas <span style="color:#ae81ff">3</span> -p 80:80 --name nginx nginx:latest
</span></span></code></pre></div><p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-resource-manage-05.png" alt=""></p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-resource-manage-06.png" alt=""></p>
<p>现在我们使用浏览器，输入任意节点IP，即可看到nginx默认页面。</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-resource-manage-07.png" alt=""></p>
<h4 id="查看服务">查看服务</h4>
<p>查看当前Swarm集群运行的服务</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker service ls
</span></span></code></pre></div><p>查看某个服务的详情</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker service ps nginx
</span></span></code></pre></div><p>查看某个服务的日志</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker service logs nginx
</span></span></code></pre></div><h4 id="删除服务">删除服务</h4>
<p>从Swarm集群中移除某个服务</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker service rm nginx
</span></span></code></pre></div><h3 id="资源管理">资源管理</h3>
<p>前面利用Docker Swarm快速搭建一个最小集群，也可以在集群上部署服务，但是会发现swarm中并没有提供统一入口查看节点的资源使用情况。这时我们可以用图形化管理工具Portainer帮我们管理swarm集群。</p>
<p>Portainer是Docker的图形化管理工具，提供状态显示面板、应用模板快速部署、容器镜像网络数据卷的基本操作(包括上传下载镜像、创建容器等操作)、事件日志显示、容器控制台操作、Swarm集群和服务等集中管理和操作、登陆用户管理和控制等功能。功能十分全面，基本能满足小型单位对容器管理的全部需求。</p>
<h4 id="portainer集群运行">Portainer集群运行</h4>
<p>下载Portainer镜像</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e"># 查询当前有哪些Portainer镜像</span>
</span></span><span style="display:flex;"><span>docker search portainer
</span></span><span style="display:flex;"><span>docker pull portainer/portainer
</span></span></code></pre></div><p>安装Portainer(管理节点)</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>docker run  -d -p 9000:9000 <span style="color:#ae81ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#ae81ff"></span>--name portainer --restart<span style="color:#f92672">=</span>always <span style="color:#ae81ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#ae81ff"></span>-v /var/run/docker.sock:/var/run/docker.sock <span style="color:#ae81ff">\
</span></span></span><span style="display:flex;"><span><span style="color:#ae81ff"></span>portainer/portainer
</span></span></code></pre></div><p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-resource-manage-08.png" alt=""></p>
<h4 id="portainer配置">Portainer配置</h4>
<p>设置管理员帐号密码</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-resource-manage-09.png" alt=""></p>
<p>Portainer界面内容</p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-resource-manage-10.png" alt=""></p>
<p><img src="https://ueyao.github.io/image-hosting/blog/2019/8/docker-resource-manage-11.png" alt=""></p>
<p><em>关注【小码农薛尧】，了解更多信息</em></p>

        </article>
    </div>

    <div id="disqus_thread"></div>
<script>
    window.disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "xie-yao-de-bo-ke" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>

            </div>
        </div><footer class="text-center pb-1">
    <small class="text-muted">
        
        &copy; Copyright 2024
        
        |
        <a href="https://beian.miit.gov.cn/" target="_blank">苏ICP备14012079号</a>
        <br />
        由 <a href="https://gohugo.io/" target="_blank">Hugo</a> 强力驱动
        | 主题 <a href="https://github.com/austingebauer/devise" target="_blank">Devise</a>
        <br />

    </small>
</footer></body>
</html>
